package com.mallbook.model.service;

import com.mallbook.model.exception.ServiceException;
import com.mallbook.model.pojo.Buys;
import com.mallbook.model.mapper.BuysMapper;
import com.mallbook.model.mapper.GoodsMapper;
import com.mallbook.model.utils.MybatisUtils;
import org.apache.ibatis.exceptions.PersistenceException;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-06-25
 * 购物车service层
 */
public class CartService {


    /**
     * 添加购物车商品
     *
     * @param uId   用户ID
     * @param gId   商品ID
     * @param gNums 商品数量
     */
    public void addCart(Integer uId, Integer gId, Integer gNums) {
        BuysMapper buysMapper = MybatisUtils.getMappers(BuysMapper.class);
        GoodsMapper goodsMapper = MybatisUtils.getMappers(GoodsMapper.class);
        // 加入时间：系统当前时间
        LocalDateTime date = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String setTime = formatter.format(date);

        // 判断原来数据库中是否存在该商品
        try {
            Integer num = buysMapper.findGoodsNumByuIdgId(uId, gId);
            if (num == null) {
                Buys buys = new Buys();
                buys.setUserId(uId);
                buys.setNum(gNums);
                buys.setPrice(goodsMapper.findGoodsById(gId).getSellingPrice());
                buys.setState(1);
                buys.setTimes(setTime);
                buysMapper.add(buys, gId);
            } else { // 修改库存
                Buys b = new Buys();
                b.setNum(gNums + num);
                b.setTimes(setTime);
                buysMapper.update(b, uId, gId);
            }
        } catch (
                PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 批量物理删除
     *
     * @param uId
     * @param gIds
     */
    public void delCartMore(Integer uId, List<Integer> gIds) {
        BuysMapper buysMapper = MybatisUtils.getMappers(BuysMapper.class);
        try {
            buysMapper.deleteMore(gIds, uId);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 单条物理删除
     *
     * @param uId
     * @param gId
     */
    public void delCart(Integer uId, Integer gId) {
        BuysMapper buysMapper = MybatisUtils.getMappers(BuysMapper.class);
        try {
            buysMapper.delete(gId, uId);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 查询用户的购物车信息
     *
     * @param uId 用户ID
     * @return
     */
    public List<Buys> getCartByUid(Integer uId) {
        BuysMapper buysMapper = MybatisUtils.getMappers(BuysMapper.class);
        try {
            return buysMapper.findBuysByUId(uId);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 查询用户购物车的商品总数
     *
     * @param uId
     * @return
     */
    public Integer getGoodsNumByuId(Integer uId) {
        BuysMapper buysMapper = MybatisUtils.getMappers(BuysMapper.class);
        try {
            return buysMapper.findGoodsNumByuId(uId);
        } catch (PersistenceException e) {
            e.printStackTrace();
            throw new ServiceException("数据操作异常", e);
        }
    }

    /**
     * 修改购物车
     *
     * @param nums 商品数量
     * @param uId  用户ID
     * @param gId  商品ID
     */
    public void updateCartByUidAndGid(Integer nums, Integer uId, Integer gId) {
        BuysMapper buysMapper = MybatisUtils.getMappers(BuysMapper.class);
        Buys buys = new Buys();
        buys.setNum(nums);
        try {
            buysMapper.update(buys, gId, uId);
        } catch (PersistenceException e) {
            throw new ServiceException("数据操作异常", e);
        }
    }
}
